//
//  TJTaskListController.m
//  BaoDian
//
//  Created by mac on 2024/7/28.
//

#import "TJTaskListController.h"
#import "TJTaskListCell.h"
#import "TJEmptyDataHandler.h"
#import "TJTaskDetailController.h"

@interface TJTaskListController ()

@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) TJEmptyDataHandler *emptyDataHandler;

@end

@implementation TJTaskListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.title = @"任务打卡";
    
    [self.tableView registerClass:TJTaskListCell.class forCellReuseIdentifier:@"TJTaskListCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 170;
    self.tableView.contentInset = UIEdgeInsetsMake(16, 0, 0, 0);
    self.tableView.contentOffset = CGPointMake(0, -16);
    self.tableView.backgroundColor = self.view.backgroundColor;
    [self.tableView setSectionIndexColor:UIColor.tj_9B9B9BColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
    _emptyDataHandler = [TJEmptyDataHandler emptyDataHandleWithScrollView:self.tableView configuration:^TJEmptyDataType(TJEmptyDataModel * _Nonnull dataModel) {
        dataModel.text = @"暂时还没有发布任务";
        dataModel.iconName = @"clockInRecardEmpty";
        return TJEmptyDataTypeBase;
    }];
    
    [self loadData];
}
    
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJTaskListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TJTaskListCell" forIndexPath:indexPath];
    TJTaskManageModel *model = self.datas[indexPath.row];
    cell.model = model;
    TJWeakify(self)
    [cell setTapTopViewBlock:^{
        TJTaskDetailController *taskDetailController = [[TJTaskDetailController alloc] init];
        taskDetailController.model = model;
        [weakself.navigationController pushViewController:taskDetailController];
    }];
    return cell;
}


- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)loadData {
    [TJMinePresent loadTaskListWithPageIndex:1  complete:^(NSArray * _Nonnull datas, NSError * _Nullable error) {
        if (datas.count) {
            [self.datas removeAllObjects];
            [self.datas addObjectsFromArray:datas];
        }
        self.emptyDataHandler.canDisplay = YES;
        [self.tableView reloadData];
    }];
}



@end
